define(function (require) {
var zsApp= require('app');  
require('selectCache');
zsApp.controller('resourceTypeCtrl', ['$scope', '$http', '$timeout', 'selectCache', 'promptCache', function($scope, $http, $timeout, selectCache, promptCache) {
	
	// pagination
	$scope.paginationConf = {
        currentPage     : 1,
        totalItems      : 10,
        itemsPerPage    : 10,
        pagesLength     : 15,
        perPageOptions  : [10, 20, 30, 40, 50],
        onChange: function() { 

        	var self = this,
	        	pagingData = {
	        		pageNumber  : self.currentPage,  
					pageSize 	: self.itemsPerPage,  
	        	};	

			$http
			({
				url  	: $scope.zsUrl+'digitalhome-boss/product/resouceType/getList',
				method 	: 'POST',
				data 	:  pagingData,

			}).success(function(res) {  console.log(res);

                if(res.result_code != '0000') {
                    $scope.isNoData = false;
                    return false;
                }

                // 表格数据
                $scope.tabVal = res.result;
                // 遍历每条数据对应的ID	
                angular.forEach($scope.tabVal, function(obj, i) {
                    $scope.dataID[i] = obj.resTypeId;
                });
                // 总共有多少条数据
                self.totalItems = res.length;

            }).error(function(err) {
            	$scope.isNoData = false;
            });
        }
    };	
	
	// newlyAdded: submit
	$scope.submitNewlyAdded = function() {

		var resouceTypeData = {
			name   			: $scope.resouceType_name,
			enName   		: $scope.resouceType_enName,
			resTable 		: $scope.resouceType_resTable,
			isRetail  		: $scope.resouceType_isRetail,
			description 	: $scope.resouceType_description,			
		};

		$http
		({
			url    : $scope.zsUrl+'digitalhome-boss/product/resouceType/add',
			method : 'POST',
			data   : resouceTypeData, 
 			
		}).success(function(res) {

			if( res.result_code != '0000' ) {
                promptCache.errorPrompt(res.message);
                return false;
            }

            promptCache.successPrompt(res.message);
            reloadTable();

		}).error(function() {
			promptCache.errorPrompt('新增失败！');
    		return false;
		});
	};
	
	 // details
    $scope.details = function(i) {
    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/product/resouceType/getInfo/'+$scope.dataID[i],
    		method	: 'GET',

    	}).success(function(res) {

    		if( '0000' != res.result_code ) {
    			return false;
    		}
    		
    		$scope.details_name 		= res.result.name;
    		$scope.details_enName 		= res.result.enName;
    		$scope.details_resTable 	= res.result.resTable;    	
    		$scope.details_isRetail		= res.result.isRetail;
    		$scope.details_description 	= res.result.description;    
 	
    	}).error(function(err) {
    		console.log(err);
    	});
    };
	
	 // edit: show data
	 $scope.edit = function(i) {
    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/product/resouceType/getInfo/'+$scope.dataID[i],
    		method	: 'GET',
    	}).success(function(res) {

    		if( '0000' != res.result_code ) {
    			return false;
    		}

    		$scope.index 			= i;	    		
    		$scope.edit_name 		= res.result.name;
    		$scope.edit_enName 		= res.result.enName;
    		$scope.edit_resTable 	= res.result.resTable;    	
    		$scope.edit_description = res.result.description;
    		$scope.edit_isRetail 	= res.result.isRetail;
    		$scope.editresouceTypeIsRetailSelect = 
    		[
    			{ 'id' : 0, 'name': '不可零售' },
    			{ 'id' : 1, 'name': '可零售' },
    		];	 

    	}).error(function(err) {
    		promptCache.errorPrompt('请求失败！');
    		return false;
    	});
    };

	// edit: submit data
	$scope.confirmEdit = function($event) {
         
		var editData = {
			resTypeId   	: $scope.dataID[$scope.index],
			name   			: $scope.edit_name,
			enName   		: $scope.edit_enName,
			enable 			: $scope.edit_enable,
			resTable  		: $scope.edit_resTable,
			description   	: $scope.edit_description,
			isRetail 		: $scope.edit_isRetail,
		};

		$http(
		{
			url   	: $scope.zsUrl+'digitalhome-boss/product/resouceType/modify',
			method  : 'POST',
			data    : editData, 
		}).success(function(res) {

			if( res.result_code != '0000' ) {
                promptCache.errorPrompt(res.message);
                return false;
            }

            promptCache.successPrompt(res.message);
            reloadTable();

		}).error(function(err) {
			promptCache.errorPrompt('修改失败！');
			return false;
		});
	};
	
    // delete
    $scope.getDataID = function() {

    	// data length
    	var len = $scope.dataID.length;
    	// param id
    	var whichId = 'resTypeId';
    	// param
    	var whichUrl = 'product/resouceType/delete';

    	// multiple selections 
        $scope.selectAll= function (bool, obj) {
            selectCache.selectAll(bool, $scope.dataID);
        };

        // single or multiple selections 
        $scope.select= function (selectObj, bool) {   
         	var selectId = selectObj.resTypeId;        
            selectCache.select(selectId, bool, len);    
        }; 

        // check
        $scope.check= function () {
            selectCache.check();
        };

        // delete
        $scope.delete= function () { 
            selectCache.delete(whichId, whichUrl);
            reloadTable();
        };
    };

    // reload table
    function reloadTable() { console.log(1);
        $timeout(function() {
            $scope.paginationConf.onChange();
            $scope.master = false;
        },1500);
    };
}])
.directive('resoucetypeNewlyadded', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'product/resouceType/newlyAdded.html',
		replace			: true,
	}
})
.directive('resoucetypeDetails', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'product/resouceType/details.html',
		replace			: true,
	}
})
.directive('resoucetypeEdit', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'product/resouceType/edit.html',
		replace			: true,
	}
})
.directive('resoucetypeDelete', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'product/resouceType/delete.html',
		replace			: true,
	}
})
.directive('resoucetypeTable', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'product/resouceType/table.html',
		replace			: true,
	}
});
});